package edu.tyut.game.ui.learn

import android.annotation.SuppressLint
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import edu.tyut.game.GameApplication
import edu.tyut.game.R
import edu.tyut.game.logic.model.PoemPoet
import edu.tyut.game.utils.LogUtils

class PoemAdapter(private val context: Context, private val poemList: List<PoemPoet>, private val fragment: LearnFragment):  RecyclerView.Adapter<PoemAdapter.ViewHolder>(){

    private val tag = "PoemAdapter"

    inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view){
        val title: TextView = view.findViewById(R.id.poemName)
        val poet: TextView = view.findViewById(R.id.poemPoet)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(context).inflate(R.layout.poem_item, parent, false)
        return ViewHolder(view)
    }

    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val poem = poemList[position]
        holder.title.text = poem.title
        holder.poet.text = "【${poem.dynasty}】${poem.poetName}"

        holder.itemView.setOnClickListener {
            // 跳转至对应的Fragment
            val poemPoet = poemList[position]
            LogUtils.d(tag, "跳转至对应的Fragment: $position")
            LogUtils.d(tag, poemPoet.toString())
            PoemActivity.actionStart(context, poemPoet)
            // TODO 点击发送埋点日志
            // PoemRecommend uid poemId poetId 1
            fragment.viewModel.poemRecommend("${GameApplication.loginUser?.id} ${poemPoet.poemId} ${poemPoet.poetId}")
        }
    }

    override fun getItemCount() = poemList.size
}